
#coding:utf-8

from lib import flux
from base import config
from lib.fluxext import singleton, ViewGroup

TINTS = [flux.utils.Color.FromInts(30,0,40,155),
         flux.utils.Color.FromInts(20,0,30,135),
         flux.utils.Color.FromInts(20,0,30,135),
         flux.utils.Color.FromInts(10,0,30,135),
         flux.utils.Color.FromInts(10,0,20,125),
         flux.utils.Color.FromInts(0,0,20,125),
         flux.utils.Color.FromInts(80,20,20,125),
         flux.utils.Color.FromInts(130,40,10,105),
         flux.utils.Color.FromInts(80,20,10,85),
         flux.utils.Color.FromInts(0,0,0,65),
         flux.utils.Color.FromInts(0,0,0,35),
         flux.utils.Color.FromInts(0,0,0,15),
         flux.utils.Color.FromInts(0,0,0,0),
         flux.utils.Color.FromInts(0,0,0,0),
         flux.utils.Color.FromInts(0,0,0,5),
         flux.utils.Color.FromInts(0,0,0,15),
         flux.utils.Color.FromInts(0,0,0,25),
         flux.utils.Color.FromInts(0,0,10,55),
         flux.utils.Color.FromInts(80,20,20,85),
         flux.utils.Color.FromInts(130,40,30,105),
         flux.utils.Color.FromInts(80,20,40,125),
         flux.utils.Color.FromInts(10,0,50,135),
         flux.utils.Color.FromInts(20,0,60,135),
         flux.utils.Color.FromInts(30,0,70,155)]

#NOT YET IMPLEMENTED *IGNORE*
PERIODS = [["夜晚",0,5],
           ["清晨",6,11],
           ["黄昏",12,17],
           ["夜晚",18,23]]

PER_MINUTE = 60
PER_HOUR = 60
PER_DAY = 24
PER_MONTH = 30
PER_YEAR = 12

@singleton
class GameTime(ViewGroup):

    # 开始时间，单位为s
    start_time = 25971840000

    year = 0
    month = 0
    day = 0
    hour = 0
    minute = 0
    second = 0

    def update_time(self):
        self.minute += (self.second / PER_MINUTE)
        self.second %= PER_MINUTE

        self.hour += (self.minute / PER_HOUR)
        self.minute %= PER_HOUR

        self.day += (self.hour / PER_DAY)
        self.hour %= PER_DAY

        self.month += (self.day / PER_MONTH)
        self.day %= PER_MONTH

        self.year += (self.month / PER_YEAR)
        self.month %= PER_YEAR

        self.time.SetText('%s年%02d月%02d日 %02d时%02d分' % self.get_datetime()[:-1])

    def get_datetime(self):
        return (self.year, self.month+1, self.day+1, self.hour, self.minute, self.second)

    def __init__(self):
        # 时间流速60倍，1s等于1min
        self.time_speed = 60

        self.bg = flux.View()
        self.bg.SetHUD(True)
        self.bg.SetSize(config.SCREEN_WIDTH, config.SCREEN_HEIGHT)
        self.bg.SetPos(config.SCREEN_WIDTH/2, config.SCREEN_HEIGHT/2)
        self.bg.SetColor(self.get_new_tint())

        self.time = flux.TextView('wqyMedium')
        self.time.SetColor(1,1,1)
        self.time.SetAlign(flux.ALIGN_TOPRIGHT)
        self.time.SetPos(config.SCREEN_WIDTH, 0)
        self.time.SetHUD(True)
        self.time.layer_offset = 2

        def t():
            self.second += self.time_speed
            self.update_time()
            self.bg.Anim().ColorTo(0.5, self.get_new_tint()).Do()

        self.t = flux.Timer(0.5)
        self.t.OnFire = t
        self.t.Start()

        # 当前时间，单位s
        self.second = self.start_time + 0
        self.update_time()

    def get_new_tint(self):

        min = self.minute
        game_time_hour = self.hour

        ctint = TINTS[game_time_hour]
        ntint = TINTS[0] if game_time_hour+1 == 24 else TINTS[game_time_hour+1]

        r = ctint.R - ((ctint.R - ntint.R) * (min / PER_HOUR))
        g = ctint.G - ((ctint.G - ntint.G) * (min / PER_HOUR))
        b = ctint.B - ((ctint.B - ntint.B) * (min / PER_HOUR))
        a = ctint.A - ((ctint.A - ntint.A) * (min / PER_HOUR))
        return flux.utils.Color(r,g,b,a)
